{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-24T03:12:58.532727Z",
     "start_time": "2020-10-24T03:12:58.108920Z"
    }
   },
   "outputs": [],
   "source": [
    "#coding=gbk\n",
    "import os\n",
    "import wave\n",
    "import librosa\n",
    "import librosa.output\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-24T03:11:28.265956Z",
     "start_time": "2020-10-24T03:11:28.260965Z"
    }
   },
   "outputs": [],
   "source": [
    "def add_noise(data):\n",
    "    wn = np.random.normal(0, 1, len(data))\n",
    "    data_noise = np.where(data != 0.0, data.astype('float64') + 0.02 * wn, 0.0).astype(np.float32)\n",
    "    return data_noise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-24T03:11:29.828110Z",
     "start_time": "2020-10-24T03:11:29.820114Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['apple01.wav',\n",
       " 'apple02.wav',\n",
       " 'apple03.wav',\n",
       " 'apple04.wav',\n",
       " 'apple05.wav',\n",
       " 'apple06.wav',\n",
       " 'apple07.wav',\n",
       " 'apple08.wav',\n",
       " 'apple09.wav',\n",
       " 'apple10.wav',\n",
       " 'apple11.wav',\n",
       " 'apple12.wav',\n",
       " 'apple13.wav',\n",
       " 'apple14.wav',\n",
       " 'apple15.wav']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = r\"./data/apple\"\n",
    "files = os.listdir(path)\n",
    "files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-24T03:00:18.232796Z",
     "start_time": "2020-10-24T03:00:18.223804Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./data/apple/apple02.wav\n"
     ]
    }
   ],
   "source": [
    "print(path + '/' + files[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-24T03:13:02.535954Z",
     "start_time": "2020-10-24T03:13:01.805945Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "add noise File Name is  apple01.wav\n",
      "add noise File Name is  apple02.wav\n",
      "add noise File Name is  apple03.wav\n",
      "add noise File Name is  apple04.wav\n",
      "add noise File Name is  apple05.wav\n",
      "add noise File Name is  apple06.wav\n",
      "add noise File Name is  apple07.wav\n",
      "add noise File Name is  apple08.wav\n",
      "add noise File Name is  apple09.wav\n",
      "add noise File Name is  apple10.wav\n",
      "add noise File Name is  apple11.wav\n",
      "add noise File Name is  apple12.wav\n",
      "add noise File Name is  apple13.wav\n",
      "add noise File Name is  apple14.wav\n",
      "add noise File Name is  apple15.wav\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(files)):\n",
    "    file_name = files[i]\n",
    "    print(\"add noise File Name is \", file_name)\n",
    "    data, fs = librosa.core.load(path + '/' + files[i])\n",
    "    path_noise = './data_add_noise/apple_add_noise/' + files[i][-17:-4] + '_noise.wav'\n",
    "    data_noise = add_noise(data)\n",
    "    librosa.output.write_wav(path_noise, data_noise, fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
